package com.ruoyi.basebill.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsTransferbillentry;
import com.ruoyi.basebill.mapper.OmsTransferbillMapper;
import com.ruoyi.basebill.domain.OmsTransferbill;
import com.ruoyi.basebill.service.IOmsTransferbillService;

/**
 * OMS调拨单Service业务层处理
 * 
 * @author ptt
 * @date 2023-02-10
 */
@Service
public class OmsTransferbillServiceImpl implements IOmsTransferbillService 
{
    @Autowired
    private OmsTransferbillMapper omsTransferbillMapper;
    @Autowired
    private OmsTransferbillServiceImpl transferbillService;

    /**
     * 查询OMS调拨单
     * 
     * @param id OMS调拨单主键
     * @return OMS调拨单
     */
    @Override
    public OmsTransferbill selectOmsTransferbillById(Long id)
    {
        return omsTransferbillMapper.selectOmsTransferbillById(id);
    }

    /**
     * 查询OMS调拨单列表
     * 
     * @param omsTransferbill OMS调拨单
     * @return OMS调拨单
     */
    @Override
    public List<OmsTransferbill> selectOmsTransferbillList(OmsTransferbill omsTransferbill)
    {
        return omsTransferbillMapper.selectOmsTransferbillList(omsTransferbill);
    }

    @Override
    public List<OmsTransferbill> selectOmsTransferbillDetailList(OmsTransferbill omsTransferbill) {
        return omsTransferbillMapper.selectOmsTransferbillDetailList(omsTransferbill);
    }

    /**
     * 新增OMS调拨单
     * 
     * @param omsTransferbill OMS调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsTransferbill(OmsTransferbill omsTransferbill)
    {
        int rows = omsTransferbillMapper.insertOmsTransferbill(omsTransferbill);
        transferbillService.insertOmsTransferbillentry(omsTransferbill);
        return rows;
    }

    /**
     * 修改OMS调拨单
     * 
     * @param omsTransferbill OMS调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsTransferbill(OmsTransferbill omsTransferbill)
    {
        if (omsTransferbill.getOmsTransferbillentryList() != null && omsTransferbill.getOmsTransferbillentryList().size() != 0){
            omsTransferbillMapper.deleteOmsTransferbillentryByParentid(omsTransferbill.getId());
            transferbillService.insertOmsTransferbillentry(omsTransferbill);
        }
        return omsTransferbillMapper.updateOmsTransferbill(omsTransferbill);
    }

    /**
     * 批量删除OMS调拨单
     * 
     * @param ids 需要删除的OMS调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsTransferbillByIds(Long[] ids)
    {
        omsTransferbillMapper.deleteOmsTransferbillentryByParentids(ids);
        return omsTransferbillMapper.deleteOmsTransferbillByIds(ids);
    }

    /**
     * 删除OMS调拨单信息
     * 
     * @param id OMS调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsTransferbillById(Long id)
    {
        omsTransferbillMapper.deleteOmsTransferbillentryByParentid(id);
        return omsTransferbillMapper.deleteOmsTransferbillById(id);
    }

    /**
     * 新增OM调拨单分录信息
     * 
     * @param omsTransferbill OMS调拨单对象
     */
    public void insertOmsTransferbillentry(OmsTransferbill omsTransferbill)
    {
        List<OmsTransferbillentry> omsTransferbillentryList = omsTransferbill.getOmsTransferbillentryList();
        Long id = omsTransferbill.getId();
        if (StringUtils.isNotNull(omsTransferbillentryList))
        {
            List<OmsTransferbillentry> list = new ArrayList<OmsTransferbillentry>();
            for (OmsTransferbillentry omsTransferbillentry : omsTransferbillentryList)
            {
                omsTransferbillentry.setParentid(id);
                list.add(omsTransferbillentry);
            }
            if (list.size() > 0)
            {
                omsTransferbillMapper.batchOmsTransferbillentry(list);
            }
        }
    }
}
